package org.lifeaddict.sybill;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;

public class Request {

	public static final int GET = 0;
	public static final int POST = 1;
	public static final boolean l = false;
	private int type;
	private String target, session, cookies, values;
	private SSLSocket socket;
	PrintWriter writer;
	BufferedReader reader;

	public Request(int type, String target) {
		this(type, target, null, null, null);
	}
	
	public Request(int type, String target, String session) {
		this(type, target, session, null, null);
	}
	
	public Request(int type, String target, String session, String cookies) {
		this(type, target, session, cookies, null);
	}

	public Request(int type, String target, String session, String cookies, String values) {
		this.type = type;
		this.target = target;
		this.session = session;
		this.cookies = cookies;
		this.values = values;
		this.makeSocket();
		try {
			this.writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream())));
			this.reader = new BufferedReader(new InputStreamReader(this.socket.getInputStream(), "UTF8"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.sendRequest();
	}
	
	public BufferedReader getReader() {
		return reader;
	}
	
	public ArrayList<String> getListAndClose() {
		ArrayList<String> out = Parser.readToListAll(reader);
		this.closeRequest();
		return out;
	}

	public void closeRequest() {
		try {
			this.reader.close();
			this.writer.close();
			this.socket.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void sendRequest() {
		try {
			if (l) System.out.println(this.typeString() + " /mu_online/" + this.target + " HTTP/1.1");
			this.writer.println(this.typeString() + " /mu_online/" + this.target + " HTTP/1.1");
			if (l) System.out.println("Host: online.unileoben.ac.at");
			this.writer.println("Host: online.unileoben.ac.at");
			if (l) System.out.println("User-Agent: Sybill MUonline Client/0.01alpha (Kindle Fire HD)");
			this.writer.println("User-Agent: Sybill MUonline Client/0.01alpha (Kindle Fire HD)");
			if (this.type == Request.POST && values != null) {
				this.writer.println("Content-Length: " + values.length());
				if (l) System.out.println("Content-Length: " + values.length());
			}
			if (this.session != null)
				this.cookies = "PSESSIONID=" + this.session + "; " + (this.cookies!=null?this.cookies:"");
			if (this.cookies != null)
				if (l) System.out.println("Cookie: " + this.cookies);
				this.writer.println("Cookie: " + this.cookies);
			if (this.values != null) {
				this.writer.println();
				if (l) System.out.println();
				this.writer.println(values);
				if (l) System.out.println(values);
			}
			this.writer.println();
			this.writer.flush();
			if (this.writer.checkError())
				System.out.println("SSLSocketClient:  java.io.PrintWriter error");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void makeSocket() {
		SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
		this.socket = null;
		try {
			this.socket = (SSLSocket) factory.createSocket("online.unileoben.ac.at", 443);
			this.socket.startHandshake();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private String typeString() {
		return this.type == 0 ? "GET" : "POST";
	}
	

}
